package goutil

import (
	"math"
)

// https://github.com/esimov/pigo/blob/master/core/utils.go

// abs returns the absolute value of the provided number
func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

// min returns the minum value between two numbers
func Min(val1, val2 int) int {
	if val1 < val2 {
		return val1
	}
	return val2
}

// max returns the maximum value between two numbers
func Max(val1, val2 int) int {
	if val1 > val2 {
		return val1
	}
	return val2
}

// round returns the nearest integer, rounding ties away from zero.
func Round(x float64) float64 {
	t := math.Trunc(x)
	if math.Abs(x-t) >= 0.5 {
		return t + math.Copysign(1, x)
	}
	return t
}

// pow is a fast multiply operator meant to replace the built-in math.Pow function
// for better performance, where the speed is much important than correctness.
func Pow(base float64, exp int) float64 {
	result := 1.0
	for exp > 0 {
		if exp%2 == 1 {
			result *= base
		}
		exp >>= 1
		base *= base
	}
	return result
}
// end https://github.com/esimov/pigo/blob/master/core/utils.go


// start https://hub.fastgit.xyz/ajstarks/fc/blob/master/fc.go

// MapRange -- given a value between low1 and high1, return the corresponding value between low2 and high2
func MapRange(value, low1, high1, low2, high2 float64) float64 {
	return low2 + (high2-low2)*(value-low1)/(high1-low1)
}

// Radians converts degrees to radians
func Radians(deg float64) float64 {
	return (deg * math.Pi) / 180.0
}
func Degrees(radians float64) float64 {
	return radians * 180 / math.Pi
}

// Polar returns the euclidian corrdinates from polar coordinates
func Polar(x, y, r, angle float64) (float64, float64) {
	px := (r * math.Cos(Radians(angle))) + x
	py := (r * math.Sin(Radians(angle))) + y
	return px, py
}

// PolarRadians returns the euclidian corrdinates from polar coordinates
func PolarRadians(x, y, r, angle float64) (float64, float64) {
	px := (r * math.Cos(angle)) + x
	py := (r * math.Sin(angle)) + y
	return px, py
}

func Pct(p float64, m float64) float64 {
	return ((p / 100.0) * m)
}

// dimen returns canvas dimensions from percentages (converting from x increasing left-right, y increasing top-bottom)
func Dimen(xp, yp, w, h float64) (float64, float64) {
	return Pct(xp, w), Pct(100-yp, h)
}

// end https://hub.fastgit.xyz/ajstarks/fc/blob/master/fc.go